<?php


namespace app\admin\model;


use think\Model;
use traits\model\SoftDelete;

class Jdz extends Model
{
    use SoftDelete; //软删除功能，实际项目中对数据频繁使用删除操作会导致性能问题
    protected $deleteTime = 'delete_time';  //软删除字段,ThinkPHP5的软删除功能使用时间戳类型（数据表默认值为Null）
    protected $type = ['delete_time'  =>  'datetime'];  //写入和读取数据的时候都会自动处理成时间字符串Y-m-d H:i:s的格式。

    /**
     * @return array
     * @throws \think\exception\DbException
     */
    public function getLists()
    {
        $data = input('param.');    $limit = input('limit');
        $data['status']?$where['j.status'] = $data['status']:'';
        $data['title'] && $where['title'] = ['like',"%{$data['title']}%"];
        input('complete_type')?$where['complete_type'] = input('complete_type'):'';
        if(input('reward')==1){
            $where['reward'] =['=',0];
            $where['status']=14;
        }
        if(input('reward')==2){
            $where['reward'] =['>',0];
            $where['status']=14;
        }
        if($data['begin_time']){
            $where['add_time']=['>=',$data['begin_time']];
        }
        if($data['end_time']){
            $where['add_time']=['<=',$data['end_time']];
        }
        if($data['begin_time']&&$data['end_time']){
            $where['add_time']=['between',[$data['begin_time'],$data['end_time']]];
        }
        input('reward_to')==1?$where['reward_to']=1:"";
        //halt($where);
        $list = $this
            //->fetchSql(true)
            ->alias('j')
            ->field('j.*,r.send_time')
            ->join('reward r','r.jdz_id=j.id','left')
            ->where($where)->order('add_time desc')->paginate($limit)->toArray();
        $list['code'] = 0;//用于前台数据表格判断成功字段
        return $list;
    }

    public function output(){
        $data = input('param.');
        $data['status']?$where['status'] = $data['status']:'';
        $data['title'] && $where['title'] = ['like',"%{$data['title']}%"];
        input('complete_type')?$where['complete_type'] = input('complete_type'):'';
        if(input('reward')==1){
            $where['reward'] =['=',0];
            $where['status']=14;
        }
        if(input('reward')==2){
            $where['reward'] =['>',0];
            $where['status']=14;
        }
        if($data['begin_time']){
            $where['add_time']=['>=',$data['begin_time']];
        }
        if($data['end_time']){
            $where['add_time']=['<=',$data['end_time']];
        }
        if($data['begin_time']&&$data['end_time']){
            $where['add_time']=['between',[$data['begin_time'],$data['end_time']]];
        }
		$depat = get_department_list_id();
        $depat_l =[];
        foreach ($depat['department'] as $v){
            $depat_l[$v['id']] =$v['name'];
        }
		//halt($department);
        input('reward_to')==1?$where['reward_to']=1:"";
        $list = $this->where($where)->order('add_time desc')->select();
        include_once('./public/phpexcel/PHPExcel.php');
        $PHPExcel = new \PHPExcel();
        //填入表头
        $PHPExcel->getActiveSheet()->setCellValue('A1', '序号');
        $PHPExcel->getActiveSheet()->setCellValue('B1', '标题');
        $PHPExcel->getActiveSheet()->setCellValue('C1', '提议部门');
		$PHPExcel->getActiveSheet()->setCellValue('D1', '提议人');
        $PHPExcel->getActiveSheet()->setCellValue('E1', '部门');
        $PHPExcel->getActiveSheet()->setCellValue('F1', '问题类型');
        $PHPExcel->getActiveSheet()->setCellValue('G1', '提议类型');
        $PHPExcel->getActiveSheet()->setCellValue('H1', '状态');
        $PHPExcel->getActiveSheet()->setCellValue('I1', '提议时间');
        $PHPExcel->getActiveSheet()->setCellValue('J1', '问题详情');
        $PHPExcel->getActiveSheet()->setCellValue('K1', '解决方案');
        $PHPExcel->getActiveSheet()->setCellValue('L1', '完成人员');
        $PHPExcel->getActiveSheet()->setCellValue('M1', '知识产权');
        $PHPExcel->getActiveSheet()->setCellValue('N1', '产权类型');
        $PHPExcel->getActiveSheet()->setCellValue('O1', '日志');
        $status = [
            1 => '已提交',
            2 => '已采纳',
            3 => '未采纳',
            4 => '待完成',
            5 => '已申请复活',
            6 => '未采纳',
            7 => '再次申请复活',
            8 => '总经理未采纳',
            9 => '新补充（待采纳）',
            10 => '新补充（未采纳）',
            11 => '新补充（已采纳）',
            12 => '新补充（以评估）',
            13 => '已完成',
            14 => '已验收(已关闭)',
            15 => '发布成功',
            16 => '未采纳',
            17 => '已申请复活',
            18 => '已采纳',
            19 => '任务完成',
            20 => '提议被撤回(已关闭)'
        ];
        $complete_type = [
            1 => '我提你改',
            2 => '自提自改',
            3 => '寻求合作'
        ];
        $right_type = [
            0=>'',
            1=>'专利',
            2=>"商业秘密"
        ];
        if($list){
            $i=2;
            foreach ($list as $k=>$v){
                foreach (json_decode($v['log'],true) as $value){
                    $log[] =[
                       '姓名'=>$value['user_name'],
                       '操作'=>$value['message'],
                        '内容'=>$value['content'],
                        '时间'=>date('Y-m-d H:i:s',$value['times'])
                    ];
                }
                $PHPExcel->getActiveSheet()->setCellValue('A'.$i, $v['id']);
                $PHPExcel->getActiveSheet()->setCellValue('B'.$i, $v['title']);
                $PHPExcel->getActiveSheet()->setCellValue('C'.$i, $depat_l[$v['propose_department_id']]);
				$PHPExcel->getActiveSheet()->setCellValue('D'.$i, $v['user_name']);
                $PHPExcel->getActiveSheet()->setCellValue('E'.$i, $v['department']);
                $PHPExcel->getActiveSheet()->setCellValue('F'.$i, $v['question_classfy']);
                $PHPExcel->getActiveSheet()->setCellValue('G'.$i, $complete_type[$v['complete_type']]);
                $PHPExcel->getActiveSheet()->setCellValue('H'.$i, $status[$v['status']]);
                $PHPExcel->getActiveSheet()->setCellValue('I'.$i, $v['add_time']);
                $PHPExcel->getActiveSheet()->setCellValue('J'.$i, $v['problem_content']);
                $PHPExcel->getActiveSheet()->setCellValue('K'.$i, $v['solution_content']);
                $PHPExcel->getActiveSheet()->setCellValue('L'.$i, $v['complete_user_name']);
                $PHPExcel->getActiveSheet()->setCellValue('M'.$i, $v['is_right']==1?"是":"否");
                $PHPExcel->getActiveSheet()->setCellValue('N'.$i, $right_type[$v['right_type']]);
                $PHPExcel->getActiveSheet()->setCellValue('O'.$i, json_encode($log,JSON_UNESCAPED_UNICODE));
                $i++;
                unset($log);
            }
            //设置单元格宽度
            $PHPExcel->getActiveSheet()->getColumnDimension('k')->setWidth(20);
            $objWriter = new \PHPExcel_Writer_Excel2007($PHPExcel);
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/force-download");
            header("Content-Type:application/vnd.ms-execl");
            header("Content-Type:application/octet-stream");
            header("Content-Type:application/download");
            header('Content-Disposition:attachment;filename="'.date('YmdHis').'.xlsx"');
            header("Content-Transfer-Encoding:binary");
            $objWriter->save('php://output');
        }else{
            exit("<script>alert('当前结果为空');history.go(-1);</script>");
        }
    }
    public function output_o(){
        $data = input('param.');
        $data['status']?$where['status'] = $data['status']:'';
        $data['title'] && $where['title'] = ['like',"%{$data['title']}%"];
        input('complete_type')?$where['complete_type'] = input('complete_type'):'';
        if(input('reward')==1){
            $where['reward'] =['=',0];
            $where['status']=14;
        }
        if(input('reward')==2){
            $where['reward'] =['>',0];
            $where['status']=14;
        }
        if($data['begin_time']){
            $where['add_time']=['>=',$data['begin_time']];
        }
        if($data['end_time']){
            $where['add_time']=['<=',$data['end_time']];
        }
        if($data['begin_time']&&$data['end_time']){
            $where['add_time']=['between',[$data['begin_time'],$data['end_time']]];
        }
        input('reward_to')==1?$where['reward_to']=1:"";
        $list = $this->where($where)->order('add_time desc')->select();
        include_once('./public/phpexcel/PHPExcel.php');
        $PHPExcel = new \PHPExcel();
        //填入表头
        $PHPExcel->getActiveSheet()->setCellValue('A1', '序号');
        $PHPExcel->getActiveSheet()->setCellValue('B1', '标题');
        $PHPExcel->getActiveSheet()->setCellValue('C1', '内容');
        $PHPExcel->getActiveSheet()->setCellValue('D1', '提议人');
        $PHPExcel->getActiveSheet()->setCellValue('E1', '部门');
        $PHPExcel->getActiveSheet()->setCellValue('F1', '审核意见');
        $PHPExcel->getActiveSheet()->setCellValue('G1', '问题类型');
        $PHPExcel->getActiveSheet()->setCellValue('H1', '完成人员');
        $PHPExcel->getActiveSheet()->setCellValue('I1', '状态');
        $PHPExcel->getActiveSheet()->setCellValue('J1', '提议时间');
        $PHPExcel->getActiveSheet()->setCellValue('K1', '是否专利');
        $PHPExcel->getActiveSheet()->setCellValue('L1', '专利类型');
        $depat = get_department_list_id();
        $depat_l =[];
        foreach ($depat['department'] as $v){
            $depat_l[$v['id']] =$v['name'];
        }
        $status = [
            1 => '已提交',
            2 => '已采纳',
            3 => '未采纳',
            4 => '待完成',
            5 => '已申请复活',
            6 => '未采纳',
            7 => '再次申请复活',
            8 => '总经理未采纳',
            9 => '新补充（待采纳）',
            10 => '新补充（未采纳）',
            11 => '新补充（已采纳）',
            12 => '新补充（以评估）',
            13 => '已完成',
            14 => '已验收(已关闭)',
            15 => '发布成功',
            16 => '未采纳',
            17 => '已申请复活',
            18 => '已采纳',
            19 => '任务完成',
            20 => '提议被撤回(已关闭)'
        ];
        $complete_type = [
            1 => '我提你改',
            2 => '自提自改',
            3 => '寻求合作'
        ];
        $right_type = [
            0=>'',
            1=>'专利',
            2=>"商业秘密"
        ];
        if($list){
            $i=2;
            foreach ($list as $k=>$v){
                $PHPExcel->getActiveSheet()->setCellValue('A'.$i, $v['id']);
                $PHPExcel->getActiveSheet()->setCellValue('B'.$i, $v['title']);
                $PHPExcel->getActiveSheet()->setCellValue('C'.$i, $v['problem_content']);
                $PHPExcel->getActiveSheet()->setCellValue('D'.$i, $v['user_name']);
                $PHPExcel->getActiveSheet()->setCellValue('E'.$i, $depat_l[$v['propose_department_id']]);
                $PHPExcel->getActiveSheet()->setCellValue('F'.$i, $v['class_reason']);
                $PHPExcel->getActiveSheet()->setCellValue('G'.$i, $v['question_classfy']);
                $PHPExcel->getActiveSheet()->setCellValue('H'.$i, $v['complete_user_name']);
                $PHPExcel->getActiveSheet()->setCellValue('I'.$i,  $status[$v['status']]);
                $PHPExcel->getActiveSheet()->setCellValue('J'.$i, $v['add_time']);
                $PHPExcel->getActiveSheet()->setCellValue('K'.$i, $v['is_right']==1?"是":"否");
                $PHPExcel->getActiveSheet()->setCellValue('L'.$i, $right_type[$v['right_type']]);
                $i++;
                unset($log);
            }
            //设置单元格宽度
            $PHPExcel->getActiveSheet()->getColumnDimension('k')->setWidth(20);
            $objWriter = new \PHPExcel_Writer_Excel2007($PHPExcel);
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/force-download");
            header("Content-Type:application/vnd.ms-execl");
            header("Content-Type:application/octet-stream");
            header("Content-Type:application/download");
            header('Content-Disposition:attachment;filename="'.date('YmdHis').'.xlsx"');
            header("Content-Transfer-Encoding:binary");
            $objWriter->save('php://output');
        }else{
            exit("<script>alert('当前结果为空');history.go(-1);</script>");
        }
    }

}